package com.psp.park.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Objects;
import com.northend.util.AppTextUtil;
import com.northend.util.StringUtil;
import com.psp.park.controller.res.bean.RAddressDetailBean;
import com.psp.park.controller.res.bean.RAddressListBean;
import com.psp.park.model.AddressBean;
import com.psp.park.model.AreaBean;
import com.psp.park.model.expend.AddressJson;
import com.psp.park.persist.dao.AddressDao;
import com.psp.park.persist.dao.AreaDAO;
import com.psp.park.service.AddressService;
import com.psp.park.service.exception.ServiceException;

@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	AddressDao addressImpl;

	@Autowired
	AreaDAO areaImpl;

	@Override
	public PageResult<RAddressListBean> getList(int page, int pageSize, String puid, String uid, Integer type)
			throws ServiceException {
		PageResult<RAddressListBean> result = new PageResult<RAddressListBean>();
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}

		int count = addressImpl.selectCount(uid, type);
		List<AddressBean> resList = addressImpl.selectList(page, pageSize, uid, type);
		List<RAddressListBean> resData = new ArrayList<>();
		if (resList != null && resList.size() > 0) {
			for (AddressBean bean : resList) {
				RAddressListBean rb = new RAddressListBean();
				AddressJson address = JSON.parseObject(bean.getDetail(), AddressJson.class);
				rb.setAid(bean.getAid());
				rb.setAddressee(bean.getName());
				rb.setPhone(bean.getPhone());
				rb.setPcds(bean.getPcds());
				if (address != null) {
					rb.setProvince(address.getProvince());
					rb.setCity(address.getCity());
					rb.setDistrict(address.getDistrict());
					rb.setDetail(address.getDetail());
				}
				rb.setIsDefault(bean.getIsDefault() != null ? bean.getIsDefault() == 1 ? true : false : false);
				resData.add(rb);
			}
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}

	@Override
	public RAddressDetailBean getDetail(String aid) throws ServiceException {
		RAddressDetailBean result = null;
		AddressBean bean = addressImpl.selectById(aid);

		if (bean == null) {
			throw new ServiceException("object_is_not_exist", "地址");
		}

		if (bean != null) {
			result = new RAddressDetailBean();
			result.setAid(bean.getAid());
			result.setType(bean.getType());
			result.setAddressee(bean.getName());
			result.setPhone(bean.getPhone());
			result.setPcds(bean.getPcds());
			AddressJson address = JSON.parseObject(bean.getDetail(), AddressJson.class);
			if (address != null) {
				result.setDetail(address.getDetail());
			}
			result.setIsDefault(bean.getIsDefault() != null ? bean.getIsDefault() == 1 ? true : false : false);
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean save(String puid, String uid, String aid, String addressee, String phone, String detail, String pcds,
			Integer type) throws ServiceException {
		boolean flag = false;
		boolean isNew = StringUtil.isEmpty(aid);

		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}

		AddressBean bean = new AddressBean();
		bean.setAid(aid);
		bean.setUid(uid);
		bean.setName(addressee);
		bean.setPhone(phone);
		bean.setType(type);
		bean.setPcds(pcds);
		List<String> pcdList = JSON.parseArray(pcds, String.class);

		List<AreaBean> list = areaImpl.selectListByIds(pcdList);
		AddressJson address = new AddressJson();
		if (list != null && list.size() > 0) {
			for (AreaBean area : list) {
				if (area.getLevel() == 1) {
					address.setProvince(area.getName());
				} else if (area.getLevel() == 2) {
					address.setCity(area.getName());
				} else if (area.getLevel() == 3) {
					address.setDistrict(area.getName());
				}
			}
		}
		address.setDetail(detail);
		bean.setDetail(JSON.toJSONString(address));

		if (isNew) {
			bean.setAid(AppTextUtil.getPrimaryKey());
			bean.setCreateDate(new Date());
			flag = addressImpl.insert(bean) > 0;
		} else {
			AddressBean oldAddress = addressImpl.selectById(aid);
			if (oldAddress == null) {
				throw new ServiceException("object_is_not_exist", "用户地址");
			}
			if (!Objects.equal(oldAddress.getUid(), uid)) {
				throw new ServiceException("object_is_not_exist", "用户地址");
			}
			flag = addressImpl.update(bean) > 0;
		}

		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean deleteById(String puid, String aid) throws ServiceException {
		boolean flag = false;
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		flag = addressImpl.deleteById(aid) > 0;
		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

}
